// pages/my/modifyPwd/modifyPwd.js
const ins = getApp()

Page({
  data: {
    password: '',
    password_confirm: ''
  },
  valueChange(e) {
    this.setData({
      [e.currentTarget.dataset.field]: e.detail.value
    })
  },
  submit() {
    if(this.data.password != this.data.password_confirm) {
      wx.showToast({title: '两次密码不一致', icon: 'none'})
      return false
    }

    wx.showLoading({title: '请稍候', mask: true})
    ins.api.requestApi('user/reset_password', 'POST', {password: this.data.password}, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            success: () => {
              setTimeout(() => {
                wx.navigateBack({delta: 1})
              }, 1500)
            }
          })
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
            icon: 'none'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    })
  },
})